package com.cn.qgx428.lazydiary.prump.clock

import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.cn.qgx428.lazydiary.MainActivity
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.db.bean.CStikyNote
import com.cn.qgx428.lazydiary.db.dao.StikyNoteDao
import com.cn.qgx428.lazydiary.extension.getCurrenTimePrecise
import com.cn.qgx428.lazydiary.prump.BaseClock
import com.cn.qgx428.lazydiary.ui.activity.StickNoteActivity
import com.cn.qgx428.lazydiary.ui.activity.subActivity.StickNoteChangeActivity
import java.util.*

/**
 * Created by qinguanxiong on 2018/4/8.
 */
class StikyNoteClock: BaseClock() {

    override fun alarm() {
        val cs = mObject as CStikyNote
        // 设置intent
        val intent :Intent = Intent(mContext, StickNoteChangeActivity::class.java)
        val bundle: Bundle = Bundle()
        intent.putExtra(MConst.S_STICK_NOTE, cs)
        val pi = PendingIntent.getActivity(mContext, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT)
        sendNotification(pi, 1, "便利贴", cs.content!!)
    }

    override fun getAlarmTime(): String {
        return (mObject as CStikyNote)!!.noticeTime!!
    }

    override fun compete(): Any? {
        val dao = StikyNoteDao()
        val currentTime = Calendar.getInstance().getCurrenTimePrecise()
        val datas = dao.getBuilder()
                .orderBy("noticeTime", true)
                .limit(1)
                .where()
                .ge("noticeTime", currentTime)
                .query()
        if (datas!=null && datas.size>0){
            return datas[0]
        }

        return null
    }
}